<?php

namespace App\Jobs\User;

use App\Http\Controllers\Api\Traits\PayTrait;
use App\Models\User\WxUserWithdrawal;
use App\Wen\Utils\Settings;
use Illuminate\Bus\Queueable;
use Illuminate\Contracts\Queue\ShouldBeUnique;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;
use Illuminate\Support\Carbon;

class UserWithdrawalProcessJob implements ShouldQueue
{
    use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
    use PayTrait;
    public $tries = 1;
    // 此id为 序号id
    protected $withdrawal_id = 0;
    /**
     * Create a new job instance.
     *
     * @return void
     */
    public function __construct($withdrawal_id)
    {
        //
        $this->withdrawal_id = $withdrawal_id;
    }

    /**
     * Execute the job.
     *
     * @return void
     */
    public function handle()
    {

        // 先处理微信批次打款
        if(Settings::get('mch_transfer_to_weChat', 0) == 1){
            $is_get_batch_id = false;
            $provider = 'wxpay';
            $lists = [];
            $withdrawal_ids = [];
            foreach (['mini', 'mp', 'app'] as $pay_type){
                if($is_get_batch_id){
                    break;
                }
                if($pay_type == 'app'){
                    $appid = Settings::get('app_app_id', '');
                }else if($pay_type == 'mp'){
                    $appid = Settings::get('mp_app_id', '');
                }else if($pay_type == 'mini'){
                    $appid = Settings::get('app_id', '');
                }
                if(_empty_($appid)){
                    continue;
                }
                if(_empty_($this->withdrawal_id)){
                    WxUserWithdrawal::where('is_lock', 0)->where('created_at', '>', Carbon::createFromFormat('Y.m.d H:i', '2024.8.8 17:14'))->whereIn('state', [0,5])->orderBy('pay_amount', 'asc')->orderBy('id', 'desc')->limit(1)->get()->map(function ($v) use (&$lists,&$withdrawal_ids, $provider, $pay_type){
                        $lists[] = $v->normal($provider, $pay_type);
                        $withdrawal_ids[] = $v->id;
                    });
                }else{
                    $withdrawal = WxUserWithdrawal::where('is_lock', 0)->whereIn('state', [0,5])->where('id', $this->withdrawal_id)->first();
                    if($withdrawal){
                        $lists[] = $withdrawal->normal($provider, $pay_type);
                        $withdrawal_ids[] = $withdrawal->id;
                    }
                }
                if(_empty_($lists)){
                    return;
                }

                WxUserWithdrawal::whereIn('id', $withdrawal_ids)->update(['is_lock'=>1]);
                try {
                    $batch_id = $this->transferBatch($provider, $pay_type, $lists);
                    if($batch_id){
                        $is_get_batch_id = true;
                        WxUserWithdrawal::whereIn('id', $withdrawal_ids)->update([
                            'wx_batch_id' => $batch_id,
                            'state' => ['wxpay'=>3, 'alipay'=>4][$provider],
                            'is_lock' => 0
                        ]);
                    }else{
                        WxUserWithdrawal::whereIn('id', $withdrawal_ids)->update([
                            'is_lock' => 0
                        ]);
                    }
                }catch (\Exception $e){
                    WxUserWithdrawal::whereIn('id', $withdrawal_ids)->update([
                        'is_lock' => 0
                    ]);
                    _logger_(__file__, __line__, $e->getMessage());
                    break;
                }
            }
        }

    }
}
